package com.babyqiao.wallpaper.manager.controller;

import com.babyqiao.wallpaper.manager.entity.WallpaperAlbumPic;
import com.babyqiao.wallpaper.manager.controller.dto.AlbumPicQuery;
import com.babyqiao.wallpaper.manager.entity.vo.AlbumPicResVO;
import com.babyqiao.wallpaper.manager.service.AlbumPicService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.babyqiao.wallpaper.common.UnifyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author QLZ
 * @date 2020/12/10
 */
@RestController
@RequestMapping("/api/admin/albumpic")
@Api(tags = "专辑图片管理接口")
public class AlbumPicController {
    @Autowired
    AlbumPicService albumPicService;

    /**
     * 添加专辑照片
     *
     * @Author QLZ
     * @Date 2020/12/10
     * * @param albumPic:
     * * @return: com.babyqiao.wallpaper.com.babyqiao.wallpaper.common.UnifyResult
     */

    @PostMapping("/albumpic")
    @ApiOperation(value = "添加专辑图片", httpMethod = "POST")
    public UnifyResult addAlbumPic(@RequestBody List<WallpaperAlbumPic> wallpaperAlbumPic) {
        //boolean save = albumPicService.save(albumPic);
        boolean b = albumPicService.saveBatch(wallpaperAlbumPic);
        if (b) {
            return UnifyResult.success();
        }
        return UnifyResult.error();

    }

    /**
     * 带分页的条件查询 专辑图片
     *
     * @Author QLZ
     * @Date 2020/12/10
     * * @param albumPicQuery:
     * * @return: com.babyqiao.wallpaper.com.babyqiao.wallpaper.common.UnifyResult
     */

//    @ApiOperation(value = "多条件分页查询",httpMethod="POST")
//    @PostMapping("/{current}/{limit}/pic")
//    public UnifyResult getAlbumPic(@ApiParam(name = "current",value = "当前页码",required = true) @PathVariable long current,
//                                   @ApiParam(name = "limit",value = "每页查询条数",required = true) @PathVariable long limit,
//                                   @RequestBody(required = false) AlbumPicQuery albumPicQuery){
//        //构建查询参数
//        QueryWrapper<AlbumPic> albumPicQueryWrapper = albumPicService.addSelectCondition(albumPicQuery);
//        //构建分页
//        Page<AlbumPic> page=new Page<>(current,limit);
//        albumPicService.page(page,albumPicQueryWrapper);
//        //获得总数
//        long total = page.getTotal();
//        //获得数据
//        List<AlbumPic> records = page.getRecords();
//
//
//        return UnifyResult.success().data("result",records);
//    }
    @ApiOperation(value = "多条件分页查询", httpMethod = "POST")
    @PostMapping("/{current}/{limit}/pic")
    public UnifyResult getAlbumPic(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable long current,
                                   @ApiParam(name = "limit", value = "每页查询条数", required = true) @PathVariable long limit,
                                   @RequestBody(required = false) AlbumPicQuery albumPicQuery) {
        //构建查询参数
        //QueryWrapper<AlbumPic> albumPicQueryWrapper = albumPicService.addSelectCondition(albumPicQuery);
        //构建分页
        Page<AlbumPicResVO> page = new Page<>(current, limit);
        //albumPicService.page(page,albumPicQueryWrapper);
        Page<AlbumPicResVO> albumPicResVOPage = albumPicService.multipleTablesAndCondition(page, albumPicQuery);
        //获得总数
        long total = albumPicResVOPage.getTotal();
        //获得数据
        List<AlbumPicResVO> records = albumPicResVOPage.getRecords();


        return UnifyResult.success().data("result", records).data("total", total);
    }

    /**
     * 逻辑删除专辑图片
     *
     * @Author QLZ
     * @Date 2020/12/11
     * * @param id:
     * * @return: com.babyqiao.wallpaper.com.babyqiao.wallpaper.common.UnifyResult
     */

    @Caching(evict = {
            @CacheEvict(value = "HomeContent",allEntries = true),
            @CacheEvict(value = "AlbumPicContent",allEntries=true)
    })
    @ApiOperation(value = "逻辑删除专辑图片", httpMethod = "DELETE")
    @DeleteMapping("{id}")
    public UnifyResult deleteAlbumPic(@ApiParam(name = "id", value = "图片ID", required = true) @PathVariable String id) {
        boolean b = albumPicService.removeById(id);
        if (b) {
            return UnifyResult.success();
        }
        return UnifyResult.error();

    }

    /**
     * 切换专辑上下线
     */
    @ApiOperation(value = "切换专辑上下线", httpMethod = "GET")
    @GetMapping("online/{id}/{state}")
    public UnifyResult modifiyAlbumOnline(@ApiParam(name = "id", value = "专辑ID", required = true) @PathVariable String id,
                                          @ApiParam(name = "state", value = "上线状态（0=false 1=true", required = true) @PathVariable Integer state) {
        UpdateWrapper<WallpaperAlbumPic> updateWrapper = albumPicService.addUpdataCondition(id, state);
        boolean update = albumPicService.update(updateWrapper);
        if (update) {
            return UnifyResult.success().data("result", update);
        }
        return UnifyResult.error().data("result", update);


    }


}
